package com.syxbbs.forum.controller;

import com.syxbbs.forum.common.AppResult;
import com.syxbbs.forum.common.ResultCode;
import com.syxbbs.forum.exception.ApplicationException;
import com.syxbbs.forum.model.Board;
import com.syxbbs.forum.services.IBoardServive;
import jakarta.annotation.Resource;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/board")
//板块接口
public class BoardController {
    //从配置文件中读取数据，如果没有读到，默认为9。
    @Value("${syxbbs-forum.index.board-num:9}")
    private Integer indexBoardNum;

    @Resource
    IBoardServive iBoardServive;


    @RequestMapping("/topList")
    public AppResult<List<Board>> topList(){
        log.info("首页板块个数"+indexBoardNum);
        List<Board> boards = iBoardServive.selectByNum(indexBoardNum);
        if (boards==null){
            boards=new ArrayList<>();

        }
        return AppResult.success(boards);



    }

    @RequestMapping("getById")
    public AppResult<Board> getById(@RequestParam("id") @NonNull Long id){
        Board board = iBoardServive.selectById(id);
        if (board==null || board.getDeleteState()==1){
            log.warn(ResultCode.FAILED_NOT_EXISTS.toString());
            throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS.toString());

        }
        return AppResult.success(board);


    }


}
